package com.fun_ordering.service.impl;

import com.fun_ordering.components.PageResult;
import com.fun_ordering.mapper.DishTypeMapper;
import com.fun_ordering.pojo.DishType;
import com.fun_ordering.pojo.DishTypeExample;
import com.fun_ordering.service.DishTypeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 菜品类型表服务实现层
 *
 * @author XiaoPi
 */
@Service
public class DishTypeServiceImpl implements DishTypeService {

    @Autowired
    private DishTypeMapper dishTypeMapper;

    /**
     * 查询全部
     */
    @Override
    public List<DishType> findAll() {
        return dishTypeMapper.selectByExample(null);
    }

    @Override
    public List<DishType> selectDishListByType() {
        return dishTypeMapper.selectDishListByType();
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<DishType> page = (Page<DishType>) dishTypeMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(DishType dishType) {
        dishTypeMapper.insert(dishType);
    }


    /**
     * 修改
     */
    @Override
    public void update(DishType dishType) {
        dishTypeMapper.updateByPrimaryKey(dishType);
    }

    /**
     * 根据ID获取实体
     *
     * @param typeId
     * @return
     */
    @Override
    public DishType findOne(Integer typeId) {
        return dishTypeMapper.selectByPrimaryKey(typeId);
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Integer[] typeIds) {
        for (Integer typeId : typeIds) {
            dishTypeMapper.deleteByPrimaryKey(typeId);
        }
    }

    /**
     *作用：多条件分页查询，通过status排序
     *方法名：
     *参数: [dishType, pageNum, pageSize]
     *返回值: com.fun_ordering.components.PageResult
     */
    @Override
    public PageResult findPage(DishType dishType, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("status");
        DishTypeExample example = new DishTypeExample();
        DishTypeExample.Criteria criteria = example.createCriteria();

        if (dishType != null) {
            if (dishType.getTypeName() != null && dishType.getTypeName().length() > 0) {
                criteria.andTypeNameLike("%" + dishType.getTypeName() + "%");
            }
			if (dishType.getStatus() != null && dishType.getStatus()> 0) {
				criteria.andStatusEqualTo( dishType.getStatus());
				System.out.println(dishType.getStatus());
			}
        }

        Page<DishType> page = (Page<DishType>) dishTypeMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

}
